package com.zkingsoft.zkingbase.activities;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zkingsoft.rxbus.annotation.Subscribe;
import com.zkingsoft.rxbus.annotation.Tag;
import com.zkingsoft.rxbus.thread.EventThread;
import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.base.PermissionHelper;
import com.zkingsoft.zkingbase.bean.User;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.GreenDaoManager;
import com.zkingsoft.zkingbase.manager.OkHttpManager;
import com.zkingsoft.zkingbase.manager.UMManager;
import com.zkingsoft.zkingbase.model.UserLoginModel;
import com.zkingsoft.zkingbase.presenter.UserLoginPresenter;
import com.zkingsoft.zkingbase.utils.MD5Util;
import com.zkingsoft.zkingbase.utils.PreferenceUtils;
import com.zkingsoft.zkingbase.view.ILoginView;
import com.zkingsoft.zkingbase.widget.CircleImageView;
import com.zkingsoft.zkingbase.widget.NormalInputBox;

import butterknife.BindView;
import butterknife.OnClick;

/***
 * 登录activity
 * Created by ytf on 2017/03/06
 */
public class LoginActivity extends BaseActivity implements ILoginView<User>
{

    @BindView(R.id.head_img)
    CircleImageView headImg;
    @BindView(R.id.user)
    NormalInputBox user;
    @BindView(R.id.pwd)
    NormalInputBox pwd;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.qq)
    ImageView qq;
    @BindView(R.id.wx)
    ImageView wx;
    @BindView(R.id.sina)
    ImageView sina;
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.toolbar_item)
    TextView item;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private UserLoginPresenter<ILoginView, User> presenter;
    private UserLoginModel model;
    private UMManager umManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initToolBar();

        presenter = new UserLoginPresenter<>(this);
        model = new UserLoginModel();
        umManager = UMManager.newInstance();
    }

    /***
     * 使用沉浸式
     * @return
     */
    @Override
    public boolean allowTransparentStatus()
    {
        return true;
    }

    /***
     * 配置toolbar
     */
    private void initToolBar()
    {
        headImg.setImageResource(R.drawable.adidas);
        toolbar.setNavigationIcon(R.drawable.back_selector);
        toolbar.setNavigationOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
        title.setText("账户登录");

        adjustToolbar(toolbar);

        title.setTextColor(getResources().getColor(R.color.white));
        toolbar.setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_login;
    }

    @OnClick({R.id.tv_register, R.id.btn_login,R.id.qq, R.id.wx,R.id.sina})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.tv_register:
                startActivity(new Intent(this, IPhoneRegistActivity.class));
                break;
            case R.id.btn_login:
                model.login(getPhone(), getPassword(), presenter);
                break;
            case R.id.qq:
                umManager.Login(this, SHARE_MEDIA.QQ, new UMManager.UMResultCallback()
                {
                    @Override
                    public void onStart()
                    {

                    }

                    @Override
                    public void onComplete(Object result)
                    {

                    }

                    @Override
                    public void onError(Throwable e)
                    {

                    }

                    @Override
                    public void onCancel(String str)
                    {

                    }
                });
                break;
            case R.id.wx:
                break;
            case R.id.sina:
                permissionHelper.requestStoragePermission(this, new PermissionHelper.PermissionCallback()
                {
                    @Override
                    public void onSuccess()
                    {
                        Toast.makeText(LoginActivity.this, "允许", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailed()
                    {
                        Toast.makeText(LoginActivity.this, "拒绝", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    @Override
    public void loadSuccess(User data, Object obj)
    {
        GreenDaoManager manager = GreenDaoManager.getInstance(this, Constant.DB_DEFAULT);
        manager.getDaoSession().getUserDao().insert(data);

        //标记已经登录
        PreferenceUtils.put(Constant.FLAG_IS_LOGIN, true);
        //记录用户token
        PreferenceUtils.put(Constant.FLAG_TOKEN, data.token);
        //记录用户id，方便以后删除时直接根据id删除
        PreferenceUtils.put(Constant.FlAG_USER_ID, data.suId);

        Intent intent = new Intent(this, InfoActivity.class);
        intent.putExtra("userinfo", data);
        startActivity(intent);
        rxPost("loginok", new Object());
    }

    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("loginok")})
    public void loginFinish(Object obj)
    {
        finish();
    }

    @Override
    public void loadFailed(String msg, Throwable e)
    {
        toast(msg, e);
    }

    @Override
    public String getPhone()
    {
        return user.getContent();
    }

    @Override
    public String getPassword()
    {
        return MD5Util.soltMd5(pwd.getContent());
    }
}
